﻿using UnityEngine;
using System.Collections;

/// <summary>
/// Smartfox Network Handler for login.
/// </summary>
[AddComponentMenu("wsLibrary/Network/Smartfox Login Handler")]
public class wsSmartfoxLoginHandler : wsSmartfoxNetClient
{
	public string id;
	public string password;
	public bool autoLogin = false;

	public delegate void SNetEventLogin(Sfs2X.Entities.User user);
	public delegate void SNetEventError();
	public event SNetEventLogin OnLogin = delegate { };
	public event SNetEventError OnLoginError = delegate { };

	public void Login(string id, string password)
	{
		this.smartfoxManager.ConnectServer(id, password);
	}

	protected override void Init()
	{
		if (autoLogin)
			Login(id, password);
	}

	protected override void OnConnectionEvent(bool success)
	{
	}

	protected override void OnConnectionLostEvent()
	{
	}

	protected override void OnLoginEvent(Sfs2X.Entities.User user)
	{
		//this.Log("LoginHandler> Logged in successfully, user: " + user);
		OnLogin(user);
	}

	protected override void OnLoginErrorEvent()
	{
		//this.Log("LoginHandler> Login failed.");
		OnLoginError();
	}
}
